﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using DTO;

namespace DAO
{
    public class LoaiPhongDAO:AbstractDAO
    {
        public static List<LoaiPhongDTO> layDSLoaiPhong()
        {
            OleDbConnection ketNoi = null;
            List<LoaiPhongDTO> dsLoaiPhong = new List<LoaiPhongDTO>();
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "SELECT MaLoaiPhong,TenLoaiPhong,DonGia FROM LoaiPhong";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);
                OleDbDataReader boDoc = lenh.ExecuteReader();

                while(boDoc.Read())
                {
                    LoaiPhongDTO loaiPhong = new LoaiPhongDTO();
                    loaiPhong.MaLoaiPhong= boDoc.GetInt32(0);
                    if(!boDoc.IsDBNull(1))
                        loaiPhong.TenLoaiPhong= boDoc.GetString(1);
                    if(!boDoc.IsDBNull(2))
                        loaiPhong.DonGia=boDoc.GetInt32(2);
                    dsLoaiPhong.Add(loaiPhong);
                }
            }
            catch(Exception ex)
            {
                dsLoaiPhong = new List<LoaiPhongDTO>();
            }
            finally
            {
                if(ketNoi!=null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }

            return dsLoaiPhong;
        }
        public static bool thayDoi(LoaiPhongDTO loaiPhong)
        {
            bool ketQua = true;
            OleDbConnection ketNoi=null;
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "UPDATE LoaiPhong SET DonGia=@DonGia WHERE MaLoaiPhong=@MaLoaiPhong";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);
                OleDbParameter thamSo;

                thamSo = new OleDbParameter("@DonGia", OleDbType.Integer);
                thamSo.Value = loaiPhong.DonGia;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@MaLoaiPhong", OleDbType.Integer);
                thamSo.Value = loaiPhong.MaLoaiPhong;
                lenh.Parameters.Add(thamSo);

                lenh.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                ketQua = false;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return ketQua;

        }
        public static bool them(LoaiPhongDTO loaiPhong)
        {
            bool ketQua = true;
            OleDbConnection ketNoi = null;
            try
            {
                ketNoi = KetNoi();
                string chuoiLenh = "INSERT INTO LoaiPhong(TenLoaiPhong,DonGia)";
                chuoiLenh += " VALUES (@TenLoaiPhong,@DonGia)";
                OleDbCommand lenh = new OleDbCommand(chuoiLenh, ketNoi);

                OleDbParameter thamSo;
                thamSo = new OleDbParameter("@TenLoaiPhong", OleDbType.VarChar);
                thamSo.Value = loaiPhong.TenLoaiPhong;
                lenh.Parameters.Add(thamSo);

                thamSo = new OleDbParameter("@DonGia", OleDbType.VarChar);
                thamSo.Value = loaiPhong.DonGia;
                lenh.Parameters.Add(thamSo);

                lenh.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                ketQua = false;
            }
            finally
            {
                if (ketNoi != null && ketNoi.State == System.Data.ConnectionState.Open)
                    ketNoi.Close();
            }
            return ketQua;
        }
    }
}
